package com.woniuxy.three.pms.roomtype.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.woniuxy.three.pms.bean.RoomTypeBean;
import com.woniuxy.three.pms.roomtype.service.IRoomTypeService;
import com.woniuxy.three.pms.util.QiniuFileUploadUtil;
@Controller
public class RoomTypeController {

	private String imgNames;
	
	

	@Autowired
	private IRoomTypeService service;
	
	@GetMapping("/roomtype")
	@ResponseBody
	public List<RoomTypeBean> findAll(){
		List<RoomTypeBean> list = service.findAllType();
		return list;
	}
	
	@GetMapping("/roomtype1")
	@ResponseBody
	public List<RoomTypeBean> findAll1(){
		return service.findAllType1();
	}
	
	@GetMapping("/roomtype/{roomtID}")
	@ResponseBody
	public RoomTypeBean findByid(@PathVariable("roomtID")Integer roomtID) {
		System.out.println(roomtID);
		RoomTypeBean roomt = null;
		if(roomtID!=null) {
			roomt = service.findById(roomtID);
		}
		return roomt;
	}
	
	@PostMapping("/roomtype")
	@ResponseBody
	public String addRoomType(@Validated RoomTypeBean roomT,MultipartFile file[],HttpServletRequest req,BindingResult result,ModelMap map) {
		System.out.println(roomT);
		for (MultipartFile multipartFile : file) {
			System.out.println(multipartFile);
		}
		String imagename = "";
		// 设置上传的路径是D盘下的picture
		/*
		 * String imgPath = "D:/picture/";
		 * 
		 * for (MultipartFile f : file) { // 图片的名字用毫秒数+图片原来的名字拼接 String imgName =
		 * System.currentTimeMillis() + f.getOriginalFilename(); //上传文件
		 * imagename+=imgName+";";
		 * 
		 * try { uploadFileUtil(f.getBytes(), imgPath, imgName); } catch (IOException e)
		 * { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception
		 * e) { // TODO Auto-generated catch block e.printStackTrace(); } }
		 */
		try {
			imagename =  QiniuFileUploadUtil.uploadImages(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(imagename==null) {
			return "false";
		}
		if(result.hasErrors()) {
			List<FieldError> list = result.getFieldErrors();
			for (FieldError fieldError : list) {
				map.put("error_"+fieldError.getField(), fieldError.getDefaultMessage());
			}
			return "false";
		}
		roomT.setImgs(imagename);
		roomT.setStock1(0);
		roomT.setStock2(0);
		roomT.setStock3(0);
		roomT.setStock4(0);
		roomT.setStock5(0);
		roomT.setStock6(0);
		roomT.setStock7(0);
		boolean boo = service.addRoomType(roomT);
		if(boo) {
			return "true";
		}
		return "false";
	}
	
	/*
	 * @PostMapping("/roomtype")
	 * 
	 * @ResponseBody public String
	 * addRoomType(@Validated @RequestPart("roomT")RoomTypeBean
	 * roomT,@RequestPart("file")MultipartFile image,HttpServletRequest
	 * req,BindingResult result,ModelMap map) { String imagename =
	 * UploadImgUtil.uploadimg(req, image); if(result.hasErrors()) {
	 * List<FieldError> list = result.getFieldErrors(); for (FieldError fieldError :
	 * list) { map.put("error_"+fieldError.getField(),
	 * fieldError.getDefaultMessage()); } return "html/addTypeFail.html"; }
	 * roomT.setImgs(imagename); roomT.setStock1(0); roomT.setStock2(0);
	 * roomT.setStock3(0); roomT.setStock4(0); roomT.setStock5(0);
	 * roomT.setStock6(0); roomT.setStock7(0); boolean boo =
	 * service.addRoomType(roomT); if(boo) { return "true"; } return "false"; }
	 */
	
	@PutMapping("/roomtype")
	@ResponseBody
	public boolean changeRoomType( @RequestBody RoomTypeBean roomT) {
		System.out.println(roomT);
		boolean boo = service.changeRoomType(roomT);
		if(boo) {
			return true;
		}
		return false;
	}
	
	
	@DeleteMapping("/roomtype")
	@ResponseBody
	public String deleteRoomType(Integer RoomTypeId) {
		boolean boo = service.deleteRoomType(RoomTypeId);
		if(boo) {
			return "删除成功";
		}
		return "删除失败";
	}
	
	@PostMapping("/uploadImg")
	@ResponseBody
	public void uploadImg(MultipartFile file[], String areaName) throws Exception {
		String imggesNmae = null;
		System.out.println("得到的areaName:"+areaName);
		// 设置上传的路径是D盘下的picture
		String imgPath = "D:/picture/";
		
		for (MultipartFile f : file) {
			// 图片的名字用毫秒数+图片原来的名字拼接
			System.out.println(f.getSize());
			System.out.println(f.getBytes());
			String imgName = System.currentTimeMillis() + f.getOriginalFilename();
			//上传文件
			imggesNmae+=imgName+";";
			
			uploadFileUtil(f.getBytes(), imgPath, imgName);
		}
	}

	/**
	 * 上传文件的方法
	 * @param file：文件的字节
	 * @param imgPath：文件的路径
	 * @param imgName：文件的名字
	 * @throws Exception
	 */
	public void uploadFileUtil(byte[] file, String imgPath, String imgName) throws Exception {
		File targetFile = new File(imgPath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		FileOutputStream out = new FileOutputStream(imgPath + imgName);
		out.write(file);
		out.flush();
		out.close();
	}
}
